# readcsv.auk
# Tim Menzies
# Sept 12, 2012

"""

stuff

"""

@include "lib.awk"
@include "globals.awk"

function new_Rows(_Rows) {
  a(w); a(gp); a(np); a(hi); a(lo); a(all); a(d)
}
function readcsv(com, _Rows,       \
                 rows,cols,line,i) {
  com = com ? com : "cat -"
  new_Rows(_Rows)
  while((com | getline)  > 0)  {
    gsub(/[ \t\r]/,"")
    gsub(/##.*/,"")
    if ($0) {
      a(line)
      for(i=1;i<=NF;i++) line[i] = $i
      rows ? row(line,rows,cols, _Rows) : header(line,cols,_Rows)
      rows++     
    }
}}
function header(line,cols,_Rows,   x,n,i,max) {
  max = length(line)
  for(i=1;i<=max;i++) {
    x = line[i]
    if (x !~ /\~/) {
      n++
      cols[n]=i
      name[n]=x
	    w[n]   = header_weight(x)
	    gp[n]  = x ~ /!/
	    np[n]  = x ~ /\$/
	    if (np[n])
        hi[n] = -1* (lo[n]=10^32)}
}}
function header_weight(str,   w,n,tmp) {
  n=split(str,tmp,/\*/)
  return n==2 ? tmp[2]+0 : 1
}
function row(line,rows,cols,_Rows,   x,i,j,n) {
  all[rows]=rows
  if (length(line)  < length(cols))
    barph(rows ": bad " length(line) " column, not " length(cols))
  else
    for(n in cols) {
      i = cols[n]
      x = line[i]
      d[rows][n]=x
      if (x != "?")
        if (np[n]) {
          if (x > hi[n]) hi[n] = x
          if (x < lo[n]) lo[n] = x }}
}
function dumpcsv(_Rows,    rows,row,cols,col,out) {
  rows=length(d)
  for(row=1;row<=rows;row++) {
    out = d[row][1]
    cols = length(d[row])
    for(col=2;col<=cols;col++)
      out = out "," d[row][col]
    print "["out"]" }
}

function _readcsv(      _Rows,o) {
  if (args("-d,data/weather.csv,--dump,0",o)) {
    readcsv("cat " o["-d"], _Rows)
    if (o["--dump"])
      dumpcsv(_Rows) }
}

function klass1(gp,   col) {
  for(col in gp)  
    if (gp[col])
      return col
}
